package service

import (
	"github.com/gin-gonic/gin"
	"talentsystem/model"
	"github.com/astaxie/beego/orm"
)

func RequestWithdrawal(c*gin.Context){
	var param model.RecordLog
	if err:=c.Bind(&param);err!=nil {
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	o:=orm.NewOrm()
	recordlog:=new(model.RecordLog)
	recordlog.WithdrawalAmount=param.WithdrawalAmount
	recordlog.AccountId=param.AccountId
	recordlog.Way=param.Way
	recordlog.Bank=param.Bank
	recordlog.AccountNumber=param.AccountNumber
	recordlog.Date=param.Date
	_,err:=o.Insert(recordlog)
	if err!=nil {
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错误请刷新",
		})
		return
	}

	var result model.AccountHeadhunters
	err=orm.NewOrm().QueryTable("account_headhunters").Filter("id",param.AccountId).One(&result,"commission")
	if err!=nil {
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错误请刷新",
		})
		return
	}
	if result.Commission<param.WithdrawalAmount{
		c.JSON(403,gin.H{
			"status":403,
			"msg":"没有足够的佣金",
		})
		return
	}
	or:=orm.NewOrm()
	err=or.Begin()
	total,err:=or.QueryTable("account_headhunters").Filter("id",param.AccountId).Update(orm.Params{
		"Commission":result.Commission-param.WithdrawalAmount,
	})
	if err!=nil {
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错误请刷新",
		})
		return
	}
	if total==0 {
		or.Rollback()
		c.JSON(403,gin.H{
			"status":403,
			"msg":"修改失败",
		})
		return
	}
	or.Commit()

	c.JSON(200,gin.H{
		"status":200,
		"msg":1,
	})
	return
}
func GetAHHMoney(c*gin.Context){
	var param model.TypeId
	if err:=c.Bind(&param);err!=nil {
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	err,msg:=GetYourMoney(param.Id)
	if err!=nil {
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错误请刷新",
		})
		return
	}
	c.JSON(200,gin.H{
		"status":200,
		"commission":msg.Commission,
	})
}
func GetRecordLog(c*gin.Context){
	var param model.TypeId
	if err:=c.Bind(&param);err!=nil {
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	var msg []model.RecordLog
	_,err:=orm.NewOrm().QueryTable("record_log").Filter("account_id",param.Id).OrderBy("-id").All(&msg)
	if err!=nil {
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错误请刷新",
		})
		return
	}
	err,result:=GetYourMoney(param.Id)
	if err!=nil {
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错误请刷新",
		})
		return
	}
	c.JSON(200,gin.H{
		"status":200,
		"commission":result.Commission,
		"data":msg,
	})
	return
}
func GetAllRecordLog(c*gin.Context){
	var param model.GetAccountApplyJob
	if err:=c.Bind(&param);err!=nil {
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	offset:=(param.Page-1)*param.Pagesize
	var msg []model.GetRecordLog
	//_,err:=orm.NewOrm().QueryTable("record_log").Offset(offset).Limit(param.Pagesize).All(&msg)
	_,err:=orm.NewOrm().Raw("SELECT record_log.id,record_log.account_id,account_headhunters.user_name,account_headhunters.phone,record_log.bank,record_log.account_number,record_log.way,record_log.withdrawal_amount,record_log.state,record_log.date FROM record_log,account_headhunters WHERE record_log.account_id=account_headhunters.id ORDER BY record_log.id DESC LIMIT ? OFFSET ? ",param.Pagesize,offset).QueryRows(&msg)
	if err!=nil {
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错误请刷新",
		})
		return
	}
	num,err:=orm.NewOrm().QueryTable("record_log").Count()
	if err!=nil {
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错误请刷新",
		})
		return
	}
	c.JSON(200,gin.H{
		"status":200,
		"total":num,
		"data":msg,
	})
	return
}
//修改状态
func UpdateRecordLog (c*gin.Context){
	var param model.UpdateRecordLog
	if err:=c.Bind(&param);err!=nil {
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}

	if param.State==2{
		var result model.AccountHeadhunters
		err:=orm.NewOrm().QueryTable("account_headhunters").Filter("id",param.AccountId).One(&result,"commission")
		if err!=nil {
			c.JSON(500,gin.H{
				"status":500,
				"msg":"系统错误请刷新",
			})
			return
		}
		o:=orm.NewOrm()
		er:=o.Begin()
		if er!=nil {
			c.JSON(500,gin.H{
				"status":500,
				"msg":"系统错误请刷新",
			})
			return
		}
		total,err:=o.QueryTable("account_headhunters").Filter("id",param.AccountId).Update(orm.Params{
			"Commission":result.Commission+param.WithdrawalAmount,
		})
		if err!=nil {
			o.Rollback()
			c.JSON(500,gin.H{
				"status":500,
				"msg":"系统错误请刷新",
			})
			return
		}
		if total==0 {
			c.JSON(403,gin.H{
				"status":403,
				"msg":"修改失败",
			})
			return
		}
		o.Commit()
	}

	num,err:=orm.NewOrm().QueryTable("record_log").Filter("id",param.Id).Update(orm.Params{
		"State":param.State,
		"Remark":param.Remark,
	})
	if err!=nil {
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错误请刷新",
		})
		return
	}
	if num==0 {
		c.JSON(403,gin.H{
			"status":403,
			"msg":"修改失败",
		})
		return
	}
	c.JSON(200,gin.H{
		"status":200,
		"msg":1,
	})
	return
}

func GetYourMoney(id int64)(err error,msg model.AccountHeadhunters){
	err=orm.NewOrm().QueryTable("account_headhunters").Filter("id",id).One(&msg,"commission")
	return err,msg
}